package com.wzj.yygh.hosp.controller.api;

import com.wzj.yygh.hosp.service.DepartmentService;
import com.wzj.yygh.hosp.service.HospitalService;
import com.wzj.yygh.hosp.service.HospitalSetService;
import com.wzj.yygh.hosp.service.ScheduleService;
import com.wzj.yygh.hosp.utils.HttpRequestHelper;
import com.wzj.yygh.hosp.utils.Result;
import com.wzj.yygh.model.hosp.Department;
import com.wzj.yygh.model.hosp.Hospital;
import com.wzj.yygh.model.hosp.Schedule;
import com.wzj.yygh.vo.hosp.DepartmentQueryVo;
import com.wzj.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "医院管理api接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation("医院信息上传接口")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request){
        //获取和解析参数
        Map<String, Object> paramsMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //校验医院设置信息，判断医院是否被禁用，若被禁用则无法上传
        String hoscode = (String) paramsMap.get("hoscode");
        Hospital hospital = hospitalService.getByHoscode(hoscode);
//        if (hospital.getStatus() != 1){
//            return Result.fail("医院被禁用");
//        }
        String signKey = hospitalSetService.getSignKey(hoscode);

        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String)paramsMap.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        paramsMap.put("logoData",logoData);

        //校验签名
        HttpRequestHelper.checkSign(paramsMap,signKey);

        //调用业务层存储医院基本信息
        hospitalService.save(paramsMap);
        //返回结果
        return Result.ok();
    }
    //查询医院
    @ApiOperation("查询医院接口")
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        //获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //根据传递过来医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //签名校验
        HttpRequestHelper.checkSign(paramMap,signKey);

        //调用service方法实现根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }
    //删除排班
    @ApiOperation("删除排班")
    @PostMapping("schedule/remove")
    public Result remove(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号和排班编号
        String hoscode = (String)paramMap.get("hoscode");
        String hosScheduleId = (String)paramMap.get("hosScheduleId");

        //签名校验
        checkSign(paramMap);

        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }

    //查询排班接口
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号
        String hoscode = (String)paramMap.get("hoscode");
        //科室编号
        String depcode = (String)paramMap.get("depcode");
        //当前页 和 每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String)paramMap.get("limit"));
        //签名校验
        checkSign(paramMap);
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        //调用service方法
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageModel);
    }

    //上传排班接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //签名校验
        checkSign(paramMap);
        scheduleService.save(paramMap);
        return Result.ok();
    }

    //删除科室接口
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号 和 科室编号
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");
        // 签名校验
        checkSign(paramMap);
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }

    //查询科室接口
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //医院编号
        String hoscode = (String)paramMap.get("hoscode");
        //当前页 和 每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String)paramMap.get("limit"));
        //签名校验
        checkSign(paramMap);

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //调用service方法
        Page<Department> pageModel = departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageModel);
    }

    //上传科室接口
    @ApiOperation("上传科室信息")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        checkSign(paramMap);
        //调用service的方法
        departmentService.save(paramMap);
        return Result.ok();
    }

    private void checkSign(Map<String, Object> paramMap) {
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //根据传递过来医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //签名校验
        HttpRequestHelper.checkSign(paramMap,signKey);
    }

}
